<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 熊义 <34512664@qq.com>
// +----------------------------------------------------------------------
namespace app\dhgx\controller;

//use think\facade\View; // 使用模板引擎

use app\dhgx\model\ZblogModel;
use app\dhgx\model\ZbuserModel;
use cmf\controller\UserBaseController;
use think\exception\HttpResponseException;
use think\facade\Db;
use think\Response;

class GlController extends UserBaseController
{
    public function initialize()
    {

    }


    public function index()
    {
        $user =  session('user');
        if (empty($user)){
            session(null);
            $this->error('用户权限无法确定,重新登录!','/');
        }

        $userid = $user['id'];

        $zbuserm = new ZbuserModel();
        $userInfo =  $zbuserm->where('id', $userid)->find();
        if ($userInfo) {
            if ($userInfo['user_level'] > 4) {
                $this->error('用户无权限访问！');
            }else{
                $this->assign('user', $userInfo);
        return $this->fetch('index');
            }
        } else {
            //session(null);
            $this->error('用户权限无法确定,重新登录!','/');
        }
    }

    //取得当前已登录的用户总数
    public function getdlnum()
    {
        $zblogm = new ZblogModel();

        $num = $zblogm->count();
        $this->ok("ok", $num);
    }

    //取得当前在线用户数
    public function getonlineuser()
    {
        $t = time();
        $ret = Db::query("select count(*) as onlineusernum from dhgx_user where (? - last_login_time) < ? ",[$t,7200]);   //300秒为5分钟
        //[{onlineusernum: 144}]
        $this->ok("ok", $ret[0]);
    }

    //用户在线状态 更新
    public function uponlineuserstatus()
    {
        $u = session('user');

        $um = new ZbuserModel();

        $findUser = $um->where('union_id', $u['union_id'])->find();
        if ($findUser) {
            $n = $um->where('union_id', $u['union_id'])->update(['last_login_time' => time()]);
            $this->ok('union_id', $u['union_id']);
        }
        $this->err("没有找到用户!");
        
    }

    /**
     * 操作成功跳转的快捷方法
     * @access protected
     * @param mixed $msg    提示信息
     * @param mixed $data   返回的数据
     * @param array $header 发送的Header信息
     * @return void
     */
    protected function ok($msg = '', $data = '', array $header = [])
    {
        $code   = 1;
        $result = [
            'code' => $code,
            'msg'  => $msg,
            'data' => $data,
        ];

        $response = Response::create($result, "json")->header($header);

        throw new HttpResponseException($response);
    }

    /**
     * 操作错误跳转的快捷方法
     * @access protected
     * @param mixed $msg    提示信息,若要指定错误码,可以传数组,格式为['code'=>您的错误码,'msg'=>'您的错误消息']
     * @param mixed $data   返回的数据
     * @param array $header 发送的Header信息
     * @return void
     */
    protected function err($msg = '', $data = '', array $header = [])
    {
        $code = 0;
        if (is_array($msg)) {
            $code = $msg['code'];
            $msg  = $msg['msg'];
        }
        $result = [
            'code' => $code,
            'msg'  => $msg,
            'data' => $data,
        ];

        $response = Response::create($result,  "json")->header($header);

        throw new HttpResponseException($response);
    }
}
